// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.

#ifndef CEF_TESTS_CEFCLIENT_BROWSER_WALLPAPER_H_
#define CEF_TESTS_CEFCLIENT_BROWSER_WALLPAPER_H_
#pragma once
#include <string>
#include <conio.h> 
#include "protocol.h"
#include "include/internal/cef_string.h"

namespace client {

	const  int download_idle = 0;
	const int download_doing = 1;
	const int download_done = 2;

	class WallPaper {
	public:

		enum {
			NOTDOWNLOADED = 0,
			DOWNLOADED = 1,
			PLAYING = 2,
			DOWNLOADING = 3,
			PAUSEPLAY = 4,
			PAUSEDOWNLOAD = 5
		};

		WallPaper(int64 paperid, const CefString &iurl, const CefString &localpath, int state, int hasimg, LONGLONG t)
			: paperid_(paperid), iconurl_(iurl), localpreview_(localpath), state_(state), hasimg_(hasimg),ts_(t)
		{

		}

		WallPaper() { }
		void   Seticonurl(CefString &url) {
			iconurl_ = url;
		}

		void SetDownloadUrl(CefString &url) {
			downurl_ = url.ToWString();
		}

		std::wstring GetDownloadUrl() {
			return downurl_;
		}

		//void SetIconid(std::string &path) {
		//	localpreview_.FromString(path);
		//}
		void SetState(int state) {
			state_ = state;
		}

		void SetImageState(int state) {
			hasimg_ = state;
		}

		void SetPreviewPath(CefString &path) {
			localpreview_ = path;
		}

		int64 paperid_;
		CefString  iconurl_;
		CefString localpreview_;
		LONGLONG ts_;
		int state_;   // 0, not downloaded;  1, downloaded  2,  playing  3, downloading, 4 pause playing , 5, pause downloading
		int hasimg_;   // 0, no; 1, has
		std::wstring downurl_;

	};

	class StorePaper {
	public:

		//enum {
		//	DOWNLOADED = 0,
		//	PAUSE = 1,
		//	PLAYING = 2
		//};

		StorePaper(CefString themeno, const CefString &username,
			const CefString &author, int state, const CefString&tag , 
			int type, const CefString &src, const CefString &name, LONGLONG t, int themeState, long size)
			: themeno_(themeno), username_(username), author_(author), 
			state_(state), tag_(tag), themeType_(type),src_(src), name_(name), ts_(t), themeState_(themeState), 
			totalbytes_(size)
		{
			nowbytes_ = size;
		}

		StorePaper() { }
		//void SetIconid(std::string &path) {
		//	localpreview_.FromString(path);
		//}
		void SetState(int state) {
			state_ = state;
		}
 

		//int64 themeno_;
		CefString themeno_;
		CefString  username_;
		CefString author_; 
		LONGLONG ts_;
		int state_;    
		CefString tag_;
		int themeType_;
		CefString src_;
		CefString name_;
		double nowbytes_;
		double totalbytes_;
		int themeState_;
		CefString refusereason_;
	};

	/********************************************************************/
	/********************************************************************/
	class  DownloadTask {
	public:
		DownloadTask(const CefString &url,  int64 paperid, int state) {
			url_ = url;
			//imgid_ = imgid;
			paperid_ = paperid;
			state_ = state;
			//_cprintf("construct paperid = %d, state = %d\n", paperid, state);
		};

		DownloadTask(const DownloadTask &cd) {
			//_cprintf("DownloadTask copy construct %d\n", cd.paperid_);
			url_ = cd.url_;
			//imgid_ = cd.imgid_;
			paperid_ = cd.paperid_;
			state_ = cd.state_;
		}

		CefString  url_;
		//std::string  imgid_;
		int64 paperid_;
		int state_;   // 0, idle; 1, downloading; 2, finished
	};

	/*************************************************************************/
	class DownloadManager {
	public:

 

	    DownloadManager() {
			count_ = 0;
		}

		~DownloadManager() {
			clear();
		}

		void PushTask(DownloadTask &task) {
			taskarray_.push_back(task);
			count_++;
		}

		int FinishNotify( ) {
			count_--;
			for (auto v = taskarray_.begin(); v != taskarray_.end(); v++) {
				if (v->state_ == download_doing ) {
					if (LOGDEBUG)  _cprintf("update download state, done. %d\n", v->paperid_);
					v->state_ = download_done;
					return v->paperid_;
				}
			}		 
			return -1;
		}

		int Count() {
			return count_;
		}

		int GetDownloadingPaperid() {
			for (auto v = taskarray_.begin(); v != taskarray_.end(); v++) {
				if (v->state_ == download_doing) {
					return v->paperid_;
				}
			}
			return -1;
		}


		void SetPage(int i) {
			page_ = i;
		}

		int Page() {
			return page_;
		}

		void clear() {
			for (auto v = taskarray_.begin(); v != taskarray_.end();) {
				_cprintf("delete task %d\n", v->paperid_);
				v = taskarray_.erase(v);
			}
			//taskarray_.clear();
			count_ = 0;
			//std::vector<DownloadTask>   newvector;
			//taskarray_.swap(newvector);
		}

		void StartDownload(CefRefPtr<CefBrowser> browser) {
			if (count_ <= 0)  return;
			taskarray_[0].state_ = download_doing;
			if (LOGDEBUG)  _cprintf("start download   %s\n",  taskarray_[0].url_.ToString().c_str());
			browser->GetHost()->StartDownload(taskarray_[0].url_);
		}

		/**
		*  @return  -1, all task downloaded.  
		*/
		int DownLoadNext(CefRefPtr<CefBrowser> browser) {
			if (count_ <= 0) {
				clear();
				return -1;
			}
			for (auto v = taskarray_.begin(); v != taskarray_.end(); v++) {
				if (v->state_ == download_idle) {
					if (LOGDEBUG)  _cprintf("download next     %s \n",  v->url_.ToString().c_str());
					v->state_ = download_doing;
					browser->GetHost()->StartDownload(v->url_);
					break;
				}
			}
			return 0;
		}

	private:
		int count_;
		int page_;
		std::vector<DownloadTask>   taskarray_;
	};

}  // namespace client

#endif  // CEF_TESTS_CEFCLIENT_BROWSER_WALLPAPER_H_
